//
//  StatusCell.swift
//  微博项目
//
//  Created by admin on 16/3/10.
//  Copyright © 2016年 jin. All rights reserved.
//

import UIKit

class StatusCell: UITableViewCell {
    weak var statusFrame:StatusCellFrame?
        {
        didSet{
            let newValue = statusFrame
            self.headerImageView?.sd_setImageWithURL(NSURL(string: (newValue?.status.user.profile_image_url)!), placeholderImage: nil)
            self.headerNameView?.text = newValue?.status.user.name as? String
            self.headerDateView?.text = self.statusFrame?.status.getDate()
            self.headerSourceView?.text = self.statusFrame?.status.source as? String
            self.bodyTextView?.text = self.statusFrame?.status.text
            self.toolRetweetButtonView?.setTitle(self.statusFrame?.status.reposts_count, forState: UIControlState.Normal)
            self.toolCommentButtonView?.setTitle(self.statusFrame?.status.comments_count, forState: UIControlState.Normal)
            self.toolUnlikeButtonView?.setTitle(self.statusFrame?.status.attitudes_count, forState: UIControlState.Normal)
            
            
            if newValue?.status.user.isVip == false
            {
                self.headerVipView?.image = UIImage(imageLiteral: "common_icon_membership_expired")
                self.headerNameView?.textColor = UIColor.blackColor()
            }
            else
            {
                let mbrank:Int = Int((newValue?.status.user.mbrank)!)
                self.headerVipView?.image = UIImage(imageLiteral: "common_icon_membership_level\(mbrank)")
                self.headerNameView?.textColor = UIColor.redColor()
            }
            if self.statusFrame?.status.retweeted_status != nil
            {
                self.reTweetedNameView?.text = self.statusFrame?.status.retweeted_status.user?.retweetedUserName as? String
                self.reTweetedTextView?.text = self.statusFrame?.status.retweeted_status?.text
                self.reTweetedImageView?.status = self.statusFrame?.status.retweeted_status
                self.setStatusImage(self.reTweetedImageView!,imagesUrl: (self.statusFrame?.status.retweeted_status?.pic_urls)!)
            }
            else
            {
                self.reTweetedNameView?.text = ""
                self.reTweetedTextView?.text = ""
                self.bodyImageView?.status = self.statusFrame?.status
                self.setStatusImage(self.bodyImageView!,imagesUrl: (self.statusFrame?.status.pic_urls)!)
            }
            self.setFrame()
        }
    }
    /**
     头部分的view
    */
    weak var headerView:UIView?
    weak var headerImageView:UIImageView?
    weak var headerNameView:UILabel?
    weak var headerDateView:UILabel?
    weak var headerSourceView:UILabel?
    weak var headerVipView:UIImageView?
    /**
     内容部分的view
     */
    weak var bodyView:UIView?
    weak var bodyTextView:UILabel?
    weak var bodyImageView:StatusImagesView?
    /**
     转发部分
    */
    weak var reTweetedView:UIView?
    weak var reTweetedNameView:UILabel?
    weak var reTweetedTextView:UILabel?
    weak var reTweetedImageView:StatusImagesView?
    lazy var images:[UIImageView] =
    {
        var temp:[StatusImage] = []
        for var i = 0;i < 9;i++
        {
            let tempImage = StatusImage()
            tempImage.tag = i
            temp.append(tempImage)
        }
        return temp
    }()
    /**
     工具栏部分
     */
    var toolView:UIView?
    var toolRetweetButtonView:UIButton?
    var toolCommentButtonView:UIButton?
    var toolUnlikeButtonView:UIButton?
    var toolLineA:UIImageView?
    var toolLineB:UIImageView?
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    func setStatusImage(view:UIView,imagesUrl:NSArray)
    {
        for var i = 0;i < 9;i++
        {
            self.images[i].removeFromSuperview()
            if imagesUrl.count > i
            {
                self.images[i].frame = (self.statusFrame?.bodyImagesFrame![i])!
                let url:String = (imagesUrl[i] as! Picture).thumbnail_pic
                self.images[i].sd_setImageWithURL(NSURL(string: url))
                view.addSubview(self.images[i])
            }
            else
            {
                self.images[i].image = nil
            }
        }
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: UITableViewCellStyle.Default, reuseIdentifier: reuseIdentifier)
        
        let headerView = UIView()
        self.addSubview(headerView)
        headerView.backgroundColor = UIColor.whiteColor()
        self.headerView = headerView
        
        let headerImageView = UIImageView()
        headerView.addSubview(headerImageView)
        self.headerImageView = headerImageView
        
        let headerNameView = UILabel()
        headerNameView.font = UIFont.systemFontOfSize(statusCellHeaderNameFontSize)
        headerView.addSubview(headerNameView)
        self.headerNameView = headerNameView
        
        let headerDateView = UILabel()
        headerDateView.font = UIFont.systemFontOfSize(statusCellHeaderDateFontSize)
        headerDateView.textColor = UIColor.orangeColor()
        headerView.addSubview(headerDateView)
        self.headerDateView = headerDateView
        
        let headerSourceView = UILabel()
        headerSourceView.font = UIFont.systemFontOfSize(statusCellHeaderSourceFontSize)
        headerSourceView.textColor = UIColor.lightGrayColor()
        headerView.addSubview(headerSourceView)
        self.headerSourceView = headerSourceView
        
        let headerVipView = UIImageView()
        headerView.addSubview(headerVipView)
        self.headerVipView = headerVipView
        
        
        let bodyView = UIView()
        self.addSubview(bodyView)
        bodyView.backgroundColor = UIColor.whiteColor()
        self.bodyView = bodyView
        
        let bodyTextView = UILabel()
        bodyTextView.font = UIFont.systemFontOfSize(statusCellBodyTextFontSize)
        bodyTextView.numberOfLines = 0
        bodyView.addSubview(bodyTextView)
        self.bodyTextView = bodyTextView
        
        let bodyImageView = StatusImagesView()
        bodyView.addSubview(bodyImageView)
//        bodyImageView.backgroundColor = UIColor.redColor()
        self.bodyImageView = bodyImageView
        
        let reTweetedView = UIView()
        reTweetedView.backgroundColor = UIColor(patternImage: UIImage(imageLiteral: "timeline_retweet_background_highlighted"))
        bodyView.addSubview(reTweetedView)
        self.reTweetedView = reTweetedView
        

        let reTweetedNameView = UILabel()
        reTweetedNameView.font = UIFont.systemFontOfSize(statusCellHeaderNameFontSize)
        reTweetedNameView.textColor = UIColor.blueColor()
        reTweetedView.addSubview(reTweetedNameView)
        self.reTweetedNameView = reTweetedNameView
        
        let reTweetedTextView = UILabel()
        reTweetedTextView.font = UIFont.systemFontOfSize(statusCellBodyTextFontSize)
        reTweetedTextView.numberOfLines = 0
        reTweetedView.addSubview(reTweetedTextView)
        self.reTweetedTextView = reTweetedTextView
        
        let reTweetedImageView = StatusImagesView()
        reTweetedView.addSubview(reTweetedImageView)
        // bodyImageView.backgroundColor = UIColor.redColor()
        self.reTweetedImageView = reTweetedImageView
        
        let toolView = UIView()
        self.addSubview(toolView)
        self.toolView = toolView
        
        let toolRetweetButtonView = StatusToolButton(image: "timeline_icon_retweet")
        toolView.addSubview(toolRetweetButtonView)
        self.toolRetweetButtonView = toolRetweetButtonView
        
        let toolCommentButtonView = StatusToolButton(image: "timeline_icon_comment")
        toolView.addSubview(toolCommentButtonView)
        self.toolCommentButtonView = toolCommentButtonView
        
        let toolUnlikeButtonView = StatusToolButton(image: "timeline_icon_unlike")
        toolView.addSubview(toolUnlikeButtonView)
        self.toolUnlikeButtonView = toolUnlikeButtonView
        
        let toolLineA = UIImageView(image: UIImage(imageLiteral: "timeline_card_bottom_line"))
        toolView.addSubview(toolLineA)
        self.toolLineA = toolLineA
        
        let toolLineB = UIImageView(image: UIImage(imageLiteral: "timeline_card_bottom_line"))
        toolView.addSubview(toolLineB)
        self.toolLineB = toolLineB
        
        self.backgroundColor = UIColor.clearColor()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }
    func setFrame() {
//        super.layoutSubviews()
        self.headerView?.frame = self.statusFrame!.headerFrame!
        self.headerImageView?.frame = (self.statusFrame?.headerImageFrame)!
        self.headerNameView?.frame = (self.statusFrame?.headerNameFrame)!
        self.headerVipView?.frame = (self.statusFrame?.headerVipFrame)!
        
        self.headerDateView?.sizeToFit()
        self.headerDateView?.frame = CGRectMake((self.headerNameView?.frame.origin.x)!, CGRectGetMidY((self.headerNameView?.frame)!) + statusCellMargin, (self.headerDateView?.bounds.size.width)!, (self.headerDateView?.bounds.size.height)!)
        
        self.headerSourceView?.sizeToFit()
        
        let headerSourceViewFrameY = ((self.headerDateView?.frame.height)! - (self.headerSourceView?.bounds.size.height)!) + (self.headerDateView?.frame.origin.y)!
        self.headerSourceView?.frame = CGRectMake(CGRectGetMaxX((self.headerDateView?.frame)!) + statusCellMargin, headerSourceViewFrameY, (self.headerSourceView?.bounds.size.width)!, (self.headerSourceView?.bounds.size.height)!)
        
        self.bodyTextView?.frame = (self.statusFrame?.bodyTextFrame)!
        
        self.bodyView?.frame = (self.statusFrame?.bodyFrame)!
        // 转发的微博被删除做下判断，不然崩溃。。。
        if self.statusFrame?.status.retweeted_status?.user?.retweetedUserName != nil
        {
            self.reTweetedNameView?.frame = (self.statusFrame?.reTweetedNameFrame)!
        }
        else
        {
            self.reTweetedNameView?.frame = CGRect.zero
        }
        // 当有转发微博的时候调整fram
        if self.statusFrame?.status.retweeted_status != nil
        {
            self.reTweetedTextView?.frame = (self.statusFrame?.reTweetedTextFrame)!
            self.reTweetedView?.frame = (self.statusFrame?.reTweetedFrame)!
            
            if self.statusFrame?.status.retweeted_status.pic_urls.count > 0
            {
                self.reTweetedImageView?.frame = (self.statusFrame?.bodyImageViewFrame)!
            }
            else
            {
                self.reTweetedImageView?.frame = CGRect.zero
            }
        }
        else
        {
            self.reTweetedTextView?.frame = CGRect.zero
            self.reTweetedView?.frame = CGRect.zero
            self.reTweetedImageView?.frame = CGRect.zero
        }
        // 设置图片的
        if self.statusFrame?.status.pic_urls.count > 0
        {
            self.bodyImageView?.frame = (self.statusFrame?.bodyImageViewFrame)!
        }
        else
        {
            self.bodyImageView?.frame = CGRect.zero
        }
        self.toolView?.frame = (self.statusFrame?.toolFrame)!
        self.toolRetweetButtonView!.frame = (self.statusFrame?.toolRetweetFrame)!
        self.toolCommentButtonView!.frame = (self.statusFrame?.toolCommentFrame)!
        self.toolUnlikeButtonView!.frame = (self.statusFrame?.toolUnlikeFrame)!
        self.toolLineA?.frame = (self.statusFrame?.toolLineA)!
        self.toolLineB?.frame = (self.statusFrame?.toolLineB)!
    }

}
